package com.hd.crdao.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.FileUtils;
import com.hd.crdao.utils.GlobalData;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.wrapper.GlideImageUtils;
import com.soundcloud.android.crop.Crop;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 *
 *@author Administrator
 *@date 2017/6/6 0006
 */
public class EditPictureActivity extends BaseActivity {

    private static final int PHOTO_MOSAIC = 101;
    private static final int SEND_EDIT_PHOTO_SUCCESS = 103;
    private static final int PHOTO_MOSAIC_SUCCESS = 104;

    @BindView(R.id.cartype_head)
    ExpandHeadView mCartypeHead;
    @BindView(R.id.iv_pic)
    ImageView mIvPic;
    @BindView(R.id.iv_apply)
    ImageView mIvApply;
    @BindView(R.id.iv_cut)
    ImageView mIvCut;
    public String mImagePath;
    public String[] pathAndposition;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_editpicture;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        //数组的第一值是该路径的索引,第二个值是路径
        pathAndposition = getIntent().getStringArrayExtra("singleImagePath");
        mImagePath = pathAndposition[1];
        //Glide.with(UIUtils.getContext()).load(mImagePath).into(mIvPic);
        GlideImageUtils.loadImage(mImagePath,mIvPic);
    }

    @Override
    protected void initListener() {
        mCartypeHead.getLeftArrowLL().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });   //返回按钮
        mCartypeHead.getHeadViewRightText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("imagePath", new String[]{pathAndposition[0], mImagePath});
                setResult(SEND_EDIT_PHOTO_SUCCESS, intent);
                finish();
            }
        });   //完成
    }


    @OnClick({R.id.iv_apply, R.id.iv_cut})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_apply:   //涂抹
                Intent intent = new Intent(UIUtils.getContext(), DrawPhotoActivity.class);
                intent.putExtra("path", mImagePath);
                ALog.d("从编辑图片发给涂抹的路径:" + mImagePath);
                startActivityForResult(intent, PHOTO_MOSAIC);
                break;
            case R.id.iv_cut: //剪裁
                startPhotoZoom(new File(mImagePath));
                break;
        }
    }

    /**
     * 打开自带的裁剪功能
     *
     * @param
     */
    private void startPhotoZoom(File file) {
        Uri source = Uri.fromFile(file);
        File newFile = new File(GlobalData.tempImagePaht, Long.toString(System.currentTimeMillis())+".jpg");
        Uri destination = Uri.fromFile(newFile);
        Crop.of(source, destination)/*.asSquare()*/.start(this);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Crop.REQUEST_CROP && resultCode == RESULT_OK) {
            Uri output = Crop.getOutput(data);
            mImagePath = FileUtils.getRealFilePath(this, output);
            Glide.with(UIUtils.getContext()).load(output).into(mIvPic);
        } else if (requestCode == PHOTO_MOSAIC && resultCode == PHOTO_MOSAIC_SUCCESS) {
            String newPath = data.getStringExtra("imagepath");
            mImagePath = newPath;
            Glide.with(UIUtils.getContext()).load(newPath).into(mIvPic);
            ALog.d("从涂抹那边获取的路径:" + newPath + "---准备发给上传图片页面");
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}